package com.imooc.pan.server.common.mq.consumer.share;


import com.alibaba.fastjson.JSON;
//import com.imooc.pan.server.common.mq.config.RabbitMQConfig;
import com.imooc.pan.server.common.mq.event.file.DeleteFileEvent;
import com.imooc.pan.server.common.mq.event.file.RestoreFileEvent;
import com.imooc.pan.server.modules.file.entity.RPanUserFile;
import com.imooc.pan.server.modules.file.enums.DelFlagEnum;
import com.imooc.pan.server.modules.file.service.IUserFileService;
import com.imooc.pan.server.modules.share.service.IShareService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 监听文件状态变更导致分享状态变更的处理器
 */
@Component
@Slf4j
public class ShareStatusChangeConsumer {

    @Autowired
    private IUserFileService iUserFileService;

    @Autowired
    private IShareService iShareService;

    /**
     * 监听文件被删除之后，刷新所有受影响的分享的状态
     *
     * @param message
     */
    //@StreamListener(PanChannels.DELETE_FILE_INPUT)
//    @RabbitListener(queues = RabbitMQConfig.DELETE_FILE_QUEUE)
    public void changeShare2FileDeleted(String message) {
        if (Objects.isNull(message)) {
            return;
        }
        log.info("delete file message {} has been received", message);
        //if (isEmptyMessage(message)) {
        //    return;
        //}
        //printLog(message);
        DeleteFileEvent event = JSON.parseObject(message, DeleteFileEvent.class);
        //DeleteFileEvent event = message.getPayload();
        List<Long> fileIdList = event.getFileIdList();
        if (CollectionUtils.isEmpty(fileIdList)) {
            return;
        }
        List<RPanUserFile> allRecords = iUserFileService.findAllFileRecordsByFileIdList(fileIdList);
        List<Long> allAvailableFileIdList = allRecords.stream()
                .filter(record -> Objects.equals(record.getDelFlag(), DelFlagEnum.NO.getCode()))
                .map(RPanUserFile::getFileId)
                .collect(Collectors.toList());
        allAvailableFileIdList.addAll(fileIdList);
        iShareService.refreshShareStatus(allAvailableFileIdList);
    }

    /**
     * 监听文件被还原后，刷新所有受影响的分享的状态
     *
     * @param message
     */
    //@StreamListener(PanChannels.FILE_RESTORE_INPUT)
//    @RabbitListener(queues = RabbitMQConfig.RESTORE_FILE_QUEUE)
    public void changeShare2Normal(String message) {
        if (Objects.isNull(message)) {
            return;
        }
        log.info("restore file message {} has been received", message);
        //printLog(message);
        RestoreFileEvent event = JSON.parseObject(message, RestoreFileEvent.class);
        //FileRestoreEvent event = message.getPayload();
        List<Long> fileIdList = event.getFileIdList();
        if (CollectionUtils.isEmpty(fileIdList)) {
            return;
        }
        List<RPanUserFile> allRecords = iUserFileService.findAllFileRecordsByFileIdList(fileIdList);
        List<Long> allAvailableFileIdList = allRecords.stream()
                .filter(record -> Objects.equals(record.getDelFlag(), DelFlagEnum.NO.getCode()))
                .map(RPanUserFile::getFileId)
                .collect(Collectors.toList());
        allAvailableFileIdList.addAll(fileIdList);
        iShareService.refreshShareStatus(allAvailableFileIdList);
    }
}
